//
//  mmjWaterFlow.m
//  瀑布流
//
//  Created by 梅敏杰 on 2018/6/7.
//  Copyright © 2018年 梅敏杰. All rights reserved.
//

#import "mmjWaterFlow.h"
/** 默认的列数 */
static const NSInteger mmjDefaultColumnCount = 3;
/** 每一列之间的间距 */
static const CGFloat mmjDefaultColumnMargin = 10;
/** 每一行之间的间距 */
static const CGFloat mmjDefaultRowMargin = 10;
/** 边缘间距 */
static const UIEdgeInsets mmjDefaultEdgeInsets = {10, 10, 10, 10};

@interface mmjWaterFlow()
/** 内容的高度 */
@property (nonatomic,assign)CGFloat  contentHeight;
@end

@implementation mmjWaterFlow

#pragma 懒加载
-(NSMutableArray *)array{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}
- (NSMutableArray *)columnHeights{
    if (!_columnHeights) {
        _columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}

#pragma 常见数据处理
-(CGFloat)rowMargin{
    if ([self.delegate respondsToSelector:@selector(rowMarginInWaterflowLayout:)]) {
        return [self.delegate rowMarginInWaterflowLayout:self];
    }
    else{
        return mmjDefaultRowMargin;
    }
}
-(CGFloat)columMargin{
    if ([self.delegate respondsToSelector:@selector(columnMarginInWaterflowLayout:)]) {
        return [self.delegate columnMarginInWaterflowLayout:self];
    }
    else{
        return mmjDefaultColumnMargin;
    }
}
-(NSInteger)columCount{
    if ([self.delegate respondsToSelector:@selector(columnCountInWaterflowLayout:)]) {
        return [self.delegate columnCountInWaterflowLayout:self];
    }
    else{
        return mmjDefaultColumnCount;
    }
}
-(UIEdgeInsets)edgeInsets{
    if ([self.delegate respondsToSelector:@selector(edgeInsetsInWaterflowLayout:)]) {
        return [self.delegate edgeInsetsInWaterflowLayout:self];
    }
    else{
        return mmjDefaultEdgeInsets;
    }
}


//初始化
-(void)prepareLayout{
    [super prepareLayout];
    
    self.collectionView.backgroundColor = [UIColor grayColor];
    
    self.contentHeight = 0;
//清除以前计算的所有高度
    [self.columnHeights removeAllObjects];
    for (NSInteger i = 0; i < self.columCount; i++) {
        [self.columnHeights addObject:@(self.edgeInsets.top)];
    }
    
//    清除之前的所有的布局属性
    [self.array removeAllObjects];


    
    
    NSInteger count =  [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < count; i++) {
        //创建位置
        NSIndexPath * indexpath = [NSIndexPath indexPathForRow:i inSection:0];
        //获取indexpath对应的布局属性
        UICollectionViewLayoutAttributes * attrs = [self layoutAttributesForItemAtIndexPath:indexpath];
        [self.array addObject:attrs];
    }
}
//决定cell的排布
-(NSArray * )layoutAttributesForElementsInRect:(CGRect)rect{
    
   
    return self.array;
}

//调整indexpath位置cell对应的布局属性
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 创建布局属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    // collectionView的宽度
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    
    // 设置布局属性的frame
    CGFloat w = (collectionViewW - self.edgeInsets.left - self.edgeInsets.right - (self.columCount - 1) * self.columMargin) / self.columCount;
    
    CGFloat h = [self.delegate waterflowLayout:self heightForItemAtIndex:indexPath.item itemWidth:w];
 
    // 找出高度最短的那一列
    NSInteger destColumn = 0;
    CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
    
    for (NSInteger i = 1; i < self.columCount; i++) {
        // 取得第i列的高度
        CGFloat columnHeight = [self.columnHeights[i] doubleValue];
        
        if (minColumnHeight > columnHeight) {
            minColumnHeight = columnHeight;
            destColumn = i;
        }
        
    }
    
    
    
    CGFloat x = self.edgeInsets.left + destColumn * (w + self.columMargin);
    CGFloat y = minColumnHeight;
    if (y != self.edgeInsets.top) {
        y += self.rowMargin;
    }
    
    attrs.frame = CGRectMake(x, y, w, h);
    
    // 更新最短那列的高度
    self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
    
    // 记录内容的高度
    CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
    if (self.contentHeight < columnHeight) {
        self.contentHeight = columnHeight;
    }
    
    return attrs;
}

-(CGSize)collectionViewContentSize{
    CGFloat maxColumnHeight = [self.columnHeights[0] doubleValue];
    for (NSInteger i = 1; i < self.columCount; i++) {
        // 取得第i列的高度
        CGFloat columnHeight = [self.columnHeights[i] doubleValue];
        
        if (maxColumnHeight < columnHeight) {
            maxColumnHeight = columnHeight;
        }
    }
    return CGSizeMake(0, maxColumnHeight + self.edgeInsets.bottom);
}
@end
